package com.hingstd.lastwork.controllers;

import com.hingstd.lastwork.dao.CommentRepository;
import com.hingstd.lastwork.entities.Article;
import com.hingstd.lastwork.entities.Comment;
import com.hingstd.lastwork.services.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("comments")
public class CommentController {

    @Autowired
    CommentService commentService;

    @GetMapping("/new")
    public String displayCommentForm(Model model){

        Comment aComment = new Comment();
        model.addAttribute("comment", aComment);
        return "new-comment";
    }

    @PostMapping("/save")
    public String createComment(Model model, Comment comment) {
        commentService.save(comment);
        return "redirect:/comments/new";
    }
}
